package com.vic.framework.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 编码/解码工具类
 * @author VIC
 *
 */
public final class CoderUtil {

	private static final Logger logger = LoggerFactory.getLogger(CoderUtil.class);
	
	/**
	 * 对URL编码
	 */
	public static String encodeURL(String source) {
		String target;
		try{
			target = URLEncoder.encode(source, "UTF-8");
		}catch(Exception e) {
			logger.error("encoder url failure", e);
			throw new RuntimeException(e);
		}
		return target;
	}

	/**
	 * 对URL解码
	 */
	public static String decoderURL(String source) {
		String target;
		try{
			target = URLDecoder.decode(source, "UTF-8");
		}catch(Exception e) {
			logger.error("decoderURL url failure", e);
			throw new RuntimeException(e);
		}
		return target;
	}
	
	public static void main(String[] args) {
		String s ="http://www.baidu.com";
		String ta = CoderUtil.encodeURL(s);
		
		System.out.println(ta);
		
		System.out.println(CoderUtil.decoderURL(ta));
	}
	
}
